import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class WorkTest {

    public static  final Logger logger = LoggerFactory.getLogger(WorkTest.class);

    public static void main(String[] args) throws FileNotFoundException {

        Map map = new HashMap();
        map.put("name","张三");
        map.put("idno","4412811993039293023");
        getBuild("static/333.doc",map,"/Users/wsj/Desktop/word/444.doc");
    }

    private static void getBuild(String  tmpFile, Map<String, String> contentMap, String exportFile ){
        InputStream inputStream = WorkTest.class.getClassLoader().getResourceAsStream(tmpFile);
        HWPFDocument document = null;
        try {
            document = new HWPFDocument(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 读取文本内容
        Range bodyRange = document.getRange();
        // 替换内容
        for (Map.Entry<String, String> entry : contentMap.entrySet()) {
            bodyRange.replaceText("${" + entry.getKey() + "}", entry.getValue());
        }

        //导出到文件
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            document.write(byteArrayOutputStream);
            OutputStream outputStream = new FileOutputStream(exportFile);
            outputStream.write(byteArrayOutputStream.toByteArray());
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
